<template>
  <div class="invitation">
    <topbar></topbar>
    <div class="invitation_content">
      <input  class="invitation-input" :onkeyup="invitation=invitation.replace(/\W/g,'')" v-model="invitation" placeholder="请输入邀请码" :readonly=readOnly />
      <button @click="submit" class="we-btn we-btn--primary we-btn--block" :class="allow?'invitation-submit1' : 'invitation-submit2'">确定</button>
      <button class="we-btn we-btn--default we-btn--block invitation-submit3" @click="otherInvitation">其他推荐人邀请码</button>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        invitation: '',
        readOnly: true,
      }
    },
    computed: {
      allow() {
        return this.invitation === ''
      },
    },
    mounted() {
      if (this.$route.query.binding) {
        this.invitation = this.$route.query.binding
      } else {
        this.readOnly = false
      }
    },
    methods: {
      submit() {
        if (this.allow) {
          return
        }
        this.$http.post(this.$apis.user.findByInviteCode, {
          inviteCode: this.invitation,
        }).then((res) => {
          if (res.success) {
            this.$router.push({
              path: '/user/otherHome',
              query: {
                code: this.invitation,
              },
            })
          } else {
            this.$toast(res.message)
          }
        }).catch(err => {
          this.$toast(err || '邀请码无效')
        })
      },
      otherInvitation() {
        this.$router.push({
          path: '/user/otherInvitation',
        })
      },
    },
  }
</script>
